#!/usr/bin/python
# -*- coding:utf-8 -*-

#########################
#
#  版本要求：python3
#
#########################

import os
import json

# 获取当前文件（即该Python脚本）所在的目录
current_directory = os.path.dirname(__file__)

#打开并读取txt文件内容
with open(current_directory+'/tushare_price.txt', 'r') as file:
    price_txt_content = file.read()
if price_txt_content.strip() != "" :
    #将txt内容转换为JSON格式
    price_json_data = json.loads(price_txt_content)
else:
    #避免报错
    price_json_data = {}

#求出大盘指定时间和总成交量的占比
price_save_data = list(price_json_data)
avgRatio1000 = 0;
avgRatio1030 = 0;
avgRatio1130 = 0;
avgRatio1400 = 0;
avgNumRatio1000 = 0;
avgNumRatio1030 = 0;
avgNumRatio1130 = 0;
avgNumRatio1400 = 0;
for json_date in price_save_data:            
    if float(price_json_data[json_date]["15:00"]) > 0:
        ratio1000 = round(float(price_json_data[json_date]["10:00"])/float(price_json_data[json_date]["15:00"])*100,2)
        ratio1030 = round(float(price_json_data[json_date]["10:30"])/float(price_json_data[json_date]["15:00"])*100,2)
        ratio1130 = round(float(price_json_data[json_date]["11:30"])/float(price_json_data[json_date]["15:00"])*100,2)
        ratio1400 = round(float(price_json_data[json_date]["14:00"])/float(price_json_data[json_date]["15:00"])*100,2)
        if ratio1000 != 0.0:
            avgRatio1000 = avgRatio1000+ratio1000
            avgNumRatio1000 = avgNumRatio1000+1

        if ratio1030 != 0.0:
            avgRatio1030 = avgRatio1030+ratio1030
            avgNumRatio1030 = avgNumRatio1030+1

        if ratio1130 != 0.0:
            avgRatio1130 = avgRatio1130+ratio1130
            avgNumRatio1130 = avgNumRatio1130+1

        if ratio1400 != 0.0:
            avgRatio1400 = avgRatio1400+ratio1400
            avgNumRatio1400 = avgNumRatio1400+1
        
        print([json_date,ratio1000,ratio1030,ratio1130,ratio1400,price_json_data[json_date]["15:00"]])


print(['时间','10:00','10:30','11:30','14:00','15:00','all'])

print(['平均',round(avgRatio1000/avgNumRatio1000,2),round(avgRatio1030/avgNumRatio1030,2),round(avgRatio1130/avgNumRatio1130,2),round(avgRatio1400/avgNumRatio1400,2)])